const PosterBookModel = require('../db/models/posterBook');

class PosterBookService {
    //爬取海报广告图
    async addPosterBook (data) {
        const cid = data.cid;

        const result = await PosterBookModel.findOne({
            where: { cid }
        });

        if (result) {
            return await PosterBookModel.update(data, {
                where: { cid }
            })
        } else {
            return await PosterBookModel.create(data);
        }
    }

    //查询海报广告图
    async getPosterBookData () {
        return await PosterBookModel.findAll({
            attributes: {
                exclude: ['cid', 'imgUrl', 'createdAt', 'updatedAt']
            }
        });
    }

    //改变海报广告图上下架状态
    async changePosterBookStatus (id, status) {
        const ret =  await PosterBookModel.update({ status }, {
            where: { id }
        });

        return ret[0];
    }
}

module.exports = new PosterBookService();